package com.yc.io.qrcode;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;

public class ZxingTest {

	/**
	 * 二维码实现
	 * 
	 * @param msg
	 * @param path
	 */
	public static void createQrCode(String msg, String distImgPath) {
		try {
			File file = new File(distImgPath);
			OutputStream ous = new FileOutputStream(file);
			if (StringUtils.isEmpty(msg) || ous == null)
				return;
			String format = "png";
			Map<EncodeHintType, String> map = new HashMap<EncodeHintType, String>();
			// 设置编码 EncodeHintType类中可以设置MAX_SIZE，
			// ERROR_CORRECTION，CHARACTER_SET，DATA_MATRIX_SHAPE，AZTEC_LAYERS等参数
			map.put(EncodeHintType.CHARACTER_SET, "UTF-8");
			map.put(EncodeHintType.MARGIN, "2");
			// 生成二维码
			BitMatrix bitMatrix = new MultiFormatWriter().encode(msg, BarcodeFormat.QR_CODE, 200, 200, map);
			MatrixToImageWriter.writeToStream(bitMatrix, format, ous);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
//		String msg = "^_UET_^202002251587808359000001157994595900015851299590001234567";
		String msg = "^UET^1170812524a0ff1x";
		String path = "G:\\\\000workfile\\\\电子券\\\\source\\\\aaa.png";
		ZxingTest.createQrCode(msg, path);
	}
}
